<?php

namespace app\controllers\admin\uni\role;

use app\controllers\admin\base\AdminController;
use app\models\role\Permission;
use app\models\role\Role as Model;
use app\models\role\RoleMenu;

class RoleController extends AdminController
{
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find();

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    public function actionEdit()
    {
        $data = $this->post();

        try {
            if (isset($data['id']) && $data['id'] > 0) {
                $model = Model::findOne(['id' => $data['id']]);
                if (!$model) {
                    return $this->fail('操作失败');
                }
            } else {
                $model = new Model();
            }
            $data = ['Role' => $data];
            if ($model->load($data) && $model->save()) {
                return $this->success($model, '操作成功');
            } else {
                throw new \Exception($model->getFirstError('name'));
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function actionView()
    {
        $id = $this->post('id');
        $model = Model::find()
            ->where(['id' => $id])
            ->asArray()
            ->one();

        if ($model) {
            return $this->success($model);
        }

        return $this->fail('不存在该角色');
    }

    public function actionRemove()
    {
        $id = $this->post('id');

        try {
            $model = Model::findOne(['id' => $id]);
            if ($model->can_delete != '1') {
                return $this->fail('超级权限(角色)不能删除!');
            }

            if ($model->delete()) {
                return $this->success($model, '删除成功');
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    // menu list
    public function actionMenuList()
    {
        $role_id = $this->post('id');

        // role
        $role = Model::findOne(['id' => $role_id]);

        if (!$role) {
            return $this->fail();
        }

        // menu list
        $list = RoleMenu::find()
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        // checked menu
        $selected = Permission::find()
            ->where(['role_id' => $role_id])
            ->orderBy(['id' => SORT_ASC])
            ->select(['menu_id'])
            ->column();

        return $this->success([
            'list' => $list,
            'role' => $role,
            'selected' => $selected
        ]);
    }

    // save menu
    public function actionMenuSave()
    {
        $role_id = $this->post('id');
        $selected = $this->post('selected');

        if (empty($selected)) {
            return $this->fail('请勾选~~~');
        }

        // 先删除, 再增加(这里懒得做对比了)
        Permission::deleteAll(['role_id' => $role_id]);

        foreach ($selected as $v) {
            $model = new Permission();
            $model->role_id = $role_id;
            $model->menu_id = $v;
            $model->save();
        }

        return  $this->success();
    }

    // 通过 菜单/url查找角色
    public function actionRoleByMenu()
    {
        // 必须以/开头
        $url = $this->post('url');

        $menu_id = RoleMenu::find()
            ->where(['url' => $url])
            ->select(['id'])
            ->scalar();
        if ($menu_id) {
        }

        // 防止错误提示, 给一个空数组
        return $this->success([]);
    }

    // 全部返回给前端, 保存到store中
    // 在登录完之后处理
    public function actionAllRoles()
    {
        $user_id = $this->user;
        var_dump($user_id);
    }

    // 通过 用户查找角色(后端使用)
    public static function roleByUser($user_id)
    {

    }
}